package com.runvp.common.util.pdf;

import com.github.pagehelper.util.StringUtil;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.io.font.FontProgram;
import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.font.FontProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;


/**
 * 通过html去生成pdf
 */
public class PDFUtil {

    private static final Logger LOG = LoggerFactory.getLogger(PDFUtil.class);

    /**
     * 创建pdf
     * @param htmlPath
     * @param pdfPath
     * @param fontPath
     */
    public static void createPdf(String htmlPath, String pdfPath, String fontPath) {
        LOG.info("createPdf ===> param htmlPath: {},pdfPath :{},fontPath: {} ",htmlPath,pdfPath,fontPath);
        if(StringUtil.isEmpty(htmlPath) || StringUtil.isEmpty(pdfPath)  || StringUtil.isEmpty(fontPath)){
            LOG.error("createPdf ===> param is null");
            return;
        }
        try{
            // 使用itext7中的html2pdf来实现html转pdf
            ConverterProperties properties = creatBaseFont(fontPath);
            File filePath = new File(htmlPath);
            if (!filePath.getParentFile().exists()) {
                filePath.getParentFile().mkdirs();
            }
            HtmlConverter.convertToPdf(filePath, new File(pdfPath), properties);
        }catch (Exception e){
            LOG.error("createPdf ===> error:{}",e);
        }
    }

    /**
     * to pdf
     */
    public static void createPdf1(String baseUri, String htmlPath, String pdfPath,String fontPath)  {
        try {
            ConverterProperties properties =  creatBaseFont(fontPath);
            properties.setBaseUri(baseUri);
            PdfWriter writer = new PdfWriter(pdfPath);
            PdfDocument pdf = new PdfDocument(writer);
            pdf.setTagged(); //用于增加目录
//            PageSize pageSize = PageSize.A4.rotate();
//            pdf.setDefaultPageSize(pageSize);
//            MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.SCREEN);
//            mediaDeviceDescription.setWidth(pageSize.getWidth());
//            properties.setMediaDeviceDescription(mediaDeviceDescription);
            HtmlConverter.convertToPdf(new FileInputStream(htmlPath), pdf, properties);
        }catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static ConverterProperties creatBaseFont(String fontPath) {
        ConverterProperties properties = new ConverterProperties();
        FontProvider fontProvider = new DefaultFontProvider();
        FontProgram fontProgram;
        try {
            fontProgram = FontProgramFactory.createFont(fontPath);
            fontProvider.addFont(fontProgram);
            properties.setFontProvider(fontProvider);
        } catch (IOException e) {
            LOG.error("creatBaseFont ===> error:{}",e);
        }
        return properties;
    }




}
